#pragma once
#include "DHinge.h"
#include "../DWorld.h"
#include "../DVector3.h"
#include "DJointGroup.h"

namespace Skill
{
	namespace Ode
	{
		namespace Joints
		{				
			void DHinge::CollectManagedMemory()
			{
				_Anchor1 = nullptr;
				_Anchor2 = nullptr;
				_Axis = nullptr; 
				DJoint::CollectManagedMemory();
			}

			DHinge::DHinge(DWorld^ world , DJointGroup^ group)
				:DJoint(world,group)
			{
				dWorldID w = world? world->_This : NULL;
				dJointGroupID g = group? group->_This : NULL;
				_SetPointer(dJointCreateHinge(w,g),true);
			}

			DVector3^ DHinge::Anchor1::get()
			{
				if(!_Anchor1)
					_Anchor1 = gcnew DVector3();
				dJointGetHingeAnchor(_This,_Anchor1->_This);
				return _Anchor1;
			}
			void DHinge::Anchor1::set(DVector3^ value)
			{
				dJointSetHingeAnchor(_This,value->_This[0],value->_This[1],value->_This[2]);
			}

			void DHinge::SetAnchorDelta(dReal x, dReal y, dReal z, dReal ax, dReal ay, dReal az)
			{
				dJointSetHingeAnchorDelta(_This,x, y, z, ax, ay, az);
			}
			void DHinge::SetAnchorDelta(DVector3^ p, DVector3^ a)
			{
				dJointSetHingeAnchorDelta(_This,p->_This[0], p->_This[1], p->_This[2], a->_This[0], a->_This[1], a->_This[2]);
			}
			
			DVector3^ DHinge::Axis::get()
			{
				if(!_Axis)
					_Axis = gcnew DVector3();
				dJointGetHingeAxis(_This,_Axis->_This);
				return _Axis;
			}
			void DHinge::Axis::set(DVector3^ value)
			{
				dJointSetHingeAxis(_This,value->_This[0],value->_This[1],value->_This[2]);
			}

			void DHinge::SetAxisOffset(dReal x, dReal y, dReal z, dReal angle)
			{
				dJointSetHingeAxisOffset(_This,x,y,z,angle);
			}
			void DHinge::SetParam(DJointParameters parameter, dReal value)
			{
				dJointSetHingeParam(_This,(int)parameter,value);
			}
			dReal DHinge::GetParam(DJointParameters parameter)
			{
				return dJointGetHingeParam(_This,(int)parameter);
			}
			void DHinge::AddTorque(dReal torque)
			{
				dJointAddHingeTorque(_This,torque);
			}

			DVector3^ DHinge::Anchor2::get()
			{
				if(!_Anchor2)
					_Anchor2 = gcnew DVector3();
				dJointGetHingeAnchor2(_This,_Anchor2->_This);
				return _Anchor2;
			}			

			dReal DHinge::Angle::get()
			{
				return dJointGetHingeAngle(_This);
			}
			dReal DHinge::AngleRate::get()
			{
				return dJointGetHingeAngleRate(_This);
			}
		}
	}
}